<?php
class Model_Feedback_Feedback extends Model_System {
	public $_name = "feedback";
	protected $_sequence = true;
	public function getAll() {
		$result = $this->select();
		return $this->fetchAll($result)->toArray();
	}
	public function getAllIsActive() {
		$result = $this->select();
		$result->where('status = 1');
		return $this->fetchAll($result)->toArray();
	}
	public function getItemsByCate($id) {
		$result = $this->select();
		$result->where('status = 1');
		$result->where('category = ?',$id);
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();
	}
	public function getRelatedProduct($id){
		$item = $this->getItemId($id);
		$result = $this->select();
		$result->where('status = 1');
		$result->where('id != ?',$id);
		$result->where('category = ?',$item['category']);
		$result->order("id DESC");
		$result->limit(4,0);
		return $this->fetchAll($result)->toArray();
	}
	static function getNamebyId($id)
	{
		$excute = new Model_Product_Product();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['name'];
	}
	public function getRecentProduct($num){
		$result = $this->select();
		$result->where('status = 1');
		$result->order("date_cre DESC");
		$result->limit($num,0);
		return $this->fetchAll($result)->toArray();	
	}
	public function getHotProduct($num){
		$result = $this->select();
		$result->where('status = 1');
		$result->where('is_hot = 1');
		$result->order("date_cre DESC");
		$result->limit($num,0);
		return $this->fetchAll($result)->toArray();	
	}
	public function getRecentProductLimitByCate($idcat,$num){
		$result = $this->select();
		$result->where('status = 1');
		$result->where('category = ?',$idcat);
		$result->order("date_cre DESC");
		$result->limit($num,0);
		return $this->fetchAll($result)->toArray();
	}
	public function getAllStartWith($char) {
		$result = $this->select();
		$result->where('name like ?','%'.$char.'%');
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	public function getRelatedItemById($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result)->toArray();
		$query = $this->select();
		$query->where('id != ?',$data['id']);
		$query->where('status = 1');
		$query->where('category = ?',$data['category']);
		$query->order('id DESC');
		$query->limit(5,0);
		return $this->fetchAll($query)->toArray();
	
	}
	public function getLinkById($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result)->toArray();
		$modelProductCat = new Model_ProductCat_ProductCat();
		
			$aliasCat = $modelProductCat->getAliasById($data['category']);
			return '/'.$aliasCat.'/'.$data['alias'].'.html';
		
	}
	public function getItemValueByName($id,$name){
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result)->toArray();
		return $data[$name];
	}
	public function getItemAlias($alias)
	{
		$result = $this->select();
		$result->where('alias = ?',$alias);
		return $this->fetchRow($result)->toArray();
	}
	public function getItemsLanguage($lang){
		$result = $this->select();
		$result->where('language = ?',$lang);
		return $this->fetchAll($result)->toArray();
	}
	public function checkAlias($alias)
	{
		$query = $this->select();
		$query->where('alias = ?',$alias);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	public function checkAliasExceptId($id,$alias)
	{
		$query = $this->select();
		$query->where('id != ?',$id);
		$query->where('alias = ?',$alias);
		$result = $this->fetchAll($query)->toArray();
		if(count($result)!=0)
			return true;
		else
			return false;
	}
	
	public function changeStatusByCater($catergory)
	{
		$data = array('status'=>'0');
		$this->update($data,'category = '.$catergory);
	}
	public function changeStatus($id)
	{
		$query = $this->select();
		$query->where('id = ?',$id);
		$result = $this->fetchRow($query)->toArray();
		if($result['status']=='1')
		{
			$data = array('status'=>'0'); 
			$this->update($data, 'id = '.$id);
		}
		else
		{
			$data = array('status'=>'1'); 
			$this->update($data, 'id = '.$id);
		}
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}